$.fn.cateList = function () {
    var _self = this;
	var i=0;
	if($(_self).find(".hide-value").length==0){
		_self.html('<input class=".hide-value" value="" type="hidden" />');
	}
    var hideValue = _self.find(".hide-value");
    _self.setCate = function(obj,pid){
		if(obj!="" && typeof(pid)=="undefined"){
			pid=$(obj).val();
		}
        var data = _self.getData(pid);
        _self.setHtml(obj,data,'请选择...');
    }
    _self.setHtml = function(select,data,defaultName){
        var html="<option code='' value=''>"+defaultName+"</option>";
		//var html="";
		if(data != null){
			console.log(data);
			//html+="<option code='' value=''>"+defaultName+"</option>";
            $.each(data, function (k, v) {
                html+="<option code='"+v.code+"'  value='"+v.cate_id+"'>"+v.cate_name+"</option>";
            });
			
        }
		if(select!=""){
			tag=$(select).attr("i");
		}else{
			tag=i+1;
		}
		
		var l=$(_self).find("[name='cate_id_"+tag+"']").length;
		if(l==0 && data!=null){
				shtml='<select  i="'+(i+1)+'"  name="cate_id_'+i+'">'+html+'</select>';	
				_self.append(shtml);
				i++;
			}else{
				_self.find("[name='cate_id_"+tag+"']").html(html);	
		}
		if(data==null){
			$(_self).find("[name='cate_id_"+tag+"']").hide();
		}else{
			$(_self).find("[name='cate_id_"+tag+"']").show();
		}
		if(l>0){
			$(_self).find("[name='cate_id_"+tag+"']").trigger("change");
		}
		$(_self).find("select").unbind("change");
		$(_self).find("select").change(function(){
			 _self.setCate($(this));
		});	 
		
    }
    _self.getData = function(pid,func){
		$(_self).find(".hide-value").val(pid);
        var data = {};
		if(pid===""){
			return null;
		}
        $.ajax({
            type: "get",
            async :false,
            dataType:'json',
            url: "/BoxAddon/Category/Index/show_select",
            data: {type:"questions",pid: pid},
            success: function (respose) {
               /* if(respose.success){
                    data = respose.data;
					console.log(data);
					func(data);
                }*/
				//func(respose);
				data=respose;
            }
        });
		return data;
    }
    _self.getDataReverse = function(aid){
        var data = {};
        $.ajax({
            type: "get",
            async :false,
            dataType:'json',
            url: "/BoxAddon/Category/Index/get_parents",
            data: {cate_id: aid},
            success: function (respose) {
               data=respose;
            }
        });
        return data;
    }
	_self.edit=function(data){
		k=0;
		$.each(data,function(i,v){
			  var html='<select  i="'+(k+1)+'" name="cate_id_'+k+'"></select>';
			  if($(_self).find("[name='cate_id_"+k+"']").length==0){
				 $(_self).append(html);
			  }
			  $(_self).find("select").change(function(){
					 _self.setCate($(this));
				});
			  var province=$(_self).find("[name='cate_id_"+k+"']");
			  province.val(v.cate_id);
              province.trigger("change");
			  k++;
		})
		
	}
    _self.init = function(){
        _self.setCate("",0);
        var aid = parseInt($.trim(hideValue.val()));
        if(aid != 0 && !isNaN(aid)){
            var data = _self.getDataReverse(aid);
            _self.edit(data);
        }
    }
    _self.init();
    return _self;
}